package org.kernely.scheduling.administration;

import org.kernely.ui.mvp.IKernelyView;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Button;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
public class SchedulingAdminView extends VerticalLayout implements IKernelyView {

	private Table jobsTable;
	private Button addButton;
	private Button editButton;
	private Button disableButton;
	private Button refreshButton;
	
	/**
	 * Constructor of SchedulungAdminView
	 */
	public SchedulingAdminView(){
		this.addButton = new Button("Schedule new job");
		this.editButton = new Button("Edit a job");
		this.disableButton = new Button("Unschedule");
		this.refreshButton = new Button("Refresh");
		this.disableButton.setEnabled(false);
		this.editButton.setEnabled(false);
		
		HorizontalLayout buttonsLayout = new HorizontalLayout();
		
		buttonsLayout.addComponent(addButton);
		buttonsLayout.addComponent(editButton);
		buttonsLayout.addComponent(disableButton);
		buttonsLayout.addComponent(refreshButton);
		buttonsLayout.setSpacing(true);
		buttonsLayout.setMargin(true);


		
		this.jobsTable = new Table();
		this.jobsTable.setSizeFull();
		this.jobsTable.setHeight(100, Sizeable.UNITS_PERCENTAGE);
		this.jobsTable.setColumnCollapsingAllowed(true);

		
		this.jobsTable.addContainerProperty("Job", String.class, null);
		this.jobsTable.setColumnCollapsed("Job", false);
		this.jobsTable.addContainerProperty("Description", String.class,  null);
		this.jobsTable.setColumnCollapsed("Description", false);
		this.jobsTable.addContainerProperty("Start time", String.class,  null);
		this.jobsTable.setColumnCollapsed("Start time", true);
		this.jobsTable.addContainerProperty("Previous execution", String.class,  null);
		this.jobsTable.setColumnCollapsed("Previous execution", true);
		this.jobsTable.addContainerProperty("Next execution", String.class,  null);
		this.jobsTable.setColumnCollapsed("Next execution", false);
		this.jobsTable.addContainerProperty("End time", String.class,  null);
		this.jobsTable.setColumnCollapsed("End time", true);
		this.jobsTable.addContainerProperty("Cron expression", String.class,  null);
		this.jobsTable.setColumnCollapsed("Cron expression", true);
		this.jobsTable.setSelectable(true);
		this.jobsTable.setImmediate(true);
		this.jobsTable.setPageLength(20);
		
		this.jobsTable.setSortContainerPropertyId("Job");
		this.jobsTable.setSortAscending(true);
		
		
		
		// Add a listener to the table to modify state of the buttons
		this.jobsTable.addListener(new Property.ValueChangeListener() {

			/**
			 * The method called when a user select an item in the table.
			 */
			public void valueChange(ValueChangeEvent event) {
				toggleStateButtons();
			}
		});
		
		
		this.addComponent(buttonsLayout);
		this.addComponent(this.jobsTable);
		this.setSizeFull();
	}
	
	
	/**
	 * Change the state of the edit and enable/disable buttons.
	 * If nothing is selected in the table, disable the Edit and Enable/Disable buttons.
	 * If a line is selected, buttons are enabled.
	 */
	protected final void toggleStateButtons() {
		if (jobsTable.getValue() == null){
			disableButton.setEnabled(false);
			editButton.setEnabled(false);
		} else {
			disableButton.setEnabled(true);
			editButton.setEnabled(true);
		}
	}
	
	/**
	 * Gets the jobsTable
	 * @return the jobsTable
	 */
	public Table getJobsTable() {
		return jobsTable;
	}


	/**
	 * Gets the addButton
	 * @return the addButton
	 */
	public Button getAddButton() {
		return addButton;
	}


	/**
	 * Gets the refreshButton
	 * @return the refreshButton
	 */
	public Button getRefreshButton() {
		return refreshButton;
	}

	/**
	 * Gets the lockButton
	 * @return the lockButton
	 */
	public Button getDisableButton() {
		return disableButton;
	}

	/**
	 * Unused method
	 */
	public Component getComponent() {
		return this;
	}


	/**
	 * Gets the editButton
	 * @return the editButton
	 */
	public Button getEditButton() {
		return editButton;
	}
	
	

}
